package ch7.ex;

import java.util.Random;

//取钱人
//old
public class Taker2 extends Thread {
	private Accout ac;

	public Taker2(String name, Accout ac) {
		super(name);
		this.ac = ac;
	}

	@Override
	public void run() {
		Random r = new Random();

		while (this.ac.getMoney() > 0) {
			int money = r.nextInt(300);
			System.out.println("账户余额是：" + ac.getMoney() + "," + super.getName() + "准备取钱：" + money + "元");

			synchronized (ac) {
				if (this.ac.getMoney() >= money) {
					try {
						sleep(50);
					} catch (InterruptedException e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
					this.ac.withDraw(money);
					System.out.println(super.getName() + "取钱" + money + "元，现在余额是：" + ac.getMoney() + "元");
				} else {
					System.out.println("余额不足。当前余额是" + ac.getMoney() + "元，取：" + money + "元");
				}
			}
		}
	}
}
